<?php

class CLogoShow{
	
	private $db;
	
	private $keyword;
	
	private $flid;
	
	private $fromTo;
	
	private $type;
	
	private $typeTable;
	
	private $typeFlTable;
	
	private $uid;
	
	private $where;
	
	private $order;
			
	public function __construct(){
		
		$this->db=Zend_Registry::get('db');
		
	}
	
	public function setFlid($flid){
		
		$this->flid=$flid;
		
	}
	
	public function setFromTo($fromTo){
		
		$this->fromTo=$fromTo;
		
	}
	
	public function setType($type){
		
		$this->type=$type;
		
	}
	
	
	public function setTypeFlTable($typeFlTable){
		
		$this->typeFlTable=$typeFlTable;
		
	}
	
	public function setUid($uid){
		
		$this->uid=$uid;
		
	}
	
	public function setWhere($where){
		
		$this->where=$where;
		
	}
	
	public function setOrder($order){
		
		$this->order=$order;
		
	}
	
	public function setKeyWord($keyword){
		
		$this->keyword=$keyword;
		
	}
	
	public function getAllItemCounts(){
		
		$db=$this->db;
		
		$itemTable=R_PRE.'items';
		
		$select = $db->select();

		$select->from($itemTable);
		
		if($this->where!=null){
			
			$typeTable=R_PRE.$this->type;
			
			$select->join($typeTable, $itemTable.".itemid = $typeTable.itemid");
			
			$select->where($this->where);
	
		}
		
		if($this->flid!=0&&$this->flid!=null){
		
			$select->where(' flid = ? ',$this->flid);
			
		}
		
		if($this->uid!=0&&$this->uid!=null){
			
			$select->where(' uid = ? ',$this->uid);
			
		}

	        $select->where('is_showed=1');

		if($this->keyword!=null){
			
			$typeTable=R_PRE.$this->type;
			
			$select->join($typeTable, "$itemTable.itemid = $typeTable.itemid", '*');
			
			$select->where("CONCAT($itemTable.subject,$typeTable.pattern_intro,$typeTable.content) LIKE '%$this->keyword%'");
			
		}
		
		
		$select->where(' item_type = ? ',$this->type);
		
		//echo $select->__toString();

		return count($db->fetchAll($select));
		
	}
	
	public function getAllItems(){
		
		$db=$this->db;
		
		if($this->typeFlTable==null){
		
			$typeFlTable=R_PRE.$this->type."fls";
		
		}else{
			
			$typeFlTable=$this->typeFlTable;
			
		}
		
		$itemTable=R_PRE.'items';
		
		$typeTable=R_PRE.$this->type;
		
		$select = $db->select();
		
		$select->from($itemTable,'*');
		
		$select->join($typeTable, "$itemTable.itemid = $typeTable.itemid", '*');
		
		if($this->flid!=null&&$this->flid!=0){
			
			$select->join($typeFlTable, "$itemTable.flid = $typeFlTable.flid", '*');
		
			$select->where($itemTable.'.flid = ? ',$this->flid);
			
		}else{
			
			$select->joinLeft($typeFlTable, "$itemTable.flid = $typeFlTable.flid", '*');
			
		}
		
		if($this->uid!=0&&$this->uid!=null){
			
			$select->where($itemTable.'.uid = ? ',$this->uid);
			
		}else{
			
			$select->join(R_PRE.'members', R_PRE.'members.uid = '.$itemTable.'.uid', '*');
			
		}
		
                $select->where('is_showed=1');

		if($this->where!=null){
			
			$select->where($this->where);
			
		}
		
		if($this->keyword!=null){
			
			$select->where("CONCAT($itemTable.subject,$typeTable.pattern_intro,$typeTable.content) LIKE '%$this->keyword%'");
			
		}
		
		if($this->order!=null){
			
			$select->order("$this->order");
			
		}
		
		if($this->fromTo!=null){
			
			$select->limit($this->fromTo['counts'], $this->fromTo['from']);
			
		}

		return $db->fetchAll($select);
		
	}
	
}
